package com.example.library.utils;

import java.util.Comparator;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;

/**
 * author : baipb
 * desc   :
 * date   : 2020/9/28
 */
public class Encryption {

    /**
     * map 通过key升序排序
     *
     * @param map
     * @return
     */
    public static Map<String, String> sortMapByKey(Map<String, String> map) {
        if (map == null || map.isEmpty()) {
            return null;
        }
        Map<String, String> sortMap = new TreeMap<String, String>(new Comparator<String>() {

            @Override
            public int compare(String o1, String o2) {
                return o1.compareTo(o2);
            }

        });
        sortMap.putAll(map);
        return sortMap;
    }

    public static String mapToString(Map map) {
        StringBuilder sb = new StringBuilder();
        Iterator<Map.Entry<String, String>> it = map.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry<String, String> entry = it.next();
            sb.append(entry.getKey() + "=" + entry.getValue() + "&");
            System.out.println(entry.getKey() + " : " + entry.getValue());
        }
        return sb.toString();
    }
}
